package com.aggeek.springbootlearning.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.Socket;
import java.util.*;

/**
 * author:liuao
 * description:
 * Date: create on 14:07 2017/12/5
 * modify by:
 */
@Controller
@RequestMapping("/path")
public class PathController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpSession  session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        SocketFactory aDefault = SSLSocketFactory.getDefault();
        SSLSocket socket = (SSLSocket) aDefault.createSocket();
        SSLSession session1 = socket.getSession();

        String upload1 = session.getServletContext().getRealPath("upload");
        String upload2 = request.getSession().getServletContext().getRealPath("upload");

        //不带参数地址
        String requestUrl = request.getRequestURL().toString();
        //mapping部分地址
        String requestUri = request.getRequestURI();
        //参数
        String queryString = request.getQueryString();
        //
        final String remoteAddr = request.getRemoteAddr();
        final String remoteHost = request.getRemoteHost();
        int remotePort = request.getRemotePort();
        //请求方式
        String method = request.getMethod();
        String protocol = request.getProtocol();
        //
        String pathInfo = request.getPathInfo();
        //服务器地址
        String localAddr = request.getLocalAddr();
        //服务器名
        String localName = request.getLocalName();
        String authType = request.getAuthType();
        ServletContext servletContext = request.getServletContext();
        //容器中的真实地址
        String upload = servletContext.getRealPath("upload");
        String contextPath = request.getContextPath();
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String,String> map = new HashMap<>();
       while(headerNames.hasMoreElements()){
           String s = headerNames.nextElement();
           String header = request.getHeader(s);
           map.put(s,header);
       }
       //attribute需要先set才能获取到
        Enumeration<String> attributeNames = request.getAttributeNames();
        Map<String,String> map2 = new HashMap<>();
        while(attributeNames.hasMoreElements()){
            String key= attributeNames.nextElement();
            String value = request.getHeader(key);
            map.put(key,value);
        }
        String s = request.toString();
        return s;
    }
}
